//
//  FromWebsiteViewController.m
//  Qya
//
//  Created by maxwellpang on 12-9-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FromWebsiteViewController.h"
#import "MyQYaViewController.h"
#import "WebsiteViewCell.h"
#import "WebsiteIntroViewController.h"

@interface FromWebsiteViewController ()

@end

@implementation FromWebsiteViewController

@synthesize mWebsiteIntroArray;
@synthesize mTmpCell;
@synthesize mCellNib;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        mWebsiteIntroArray = [[NSMutableArray alloc] initWithObjects:@"凤凰网", @"聚美优品",@"人人网",@"腾讯网",@"小说阅读网", nil];
//        [mWebsiteIntroArray addObject:@"腾讯网"];
//        [mWebsiteIntroArray addObject:@"淘宝网"];
//        [mWebsiteIntroArray addObject:@"网易新闻"];
//        [mWebsiteIntroArray addObject:@"凤凰资讯"];
//        [mWebsiteIntroArray addObject:@"泡泡网"];
//        [mWebsiteIntroArray addObject:@"搜狐娱乐"];
//        [mWebsiteIntroArray addObject:@"中国雅虎"];
        
        websitePicArray = [[NSMutableArray alloc]initWithObjects:@"fenghuangwang", @"jumeiyouping",@"renrenwang",@"tenxunwang",@"xiaoshuoyueduwang",nil ];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(selectConfirmAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    
    UIBarButtonItem *selectAll = [[UIBarButtonItem alloc] initWithTitle:@"  全选  " style:UIBarButtonItemStyleBordered target:self action:@selector(selectAllAction:)];
    UIBarButtonItem *selectInvert = [[UIBarButtonItem alloc] initWithTitle:@"  反选  " style:UIBarButtonItemStyleBordered target:self action:@selector(selectInvert:)];
    UIBarButtonItem *selectClear = [[UIBarButtonItem alloc] initWithTitle:@"清除选择" style:UIBarButtonItemStyleBordered target:self action:@selector(selectClearAction:)];
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    [self setToolbarItems:[NSArray arrayWithObjects:flexItem, flexItem, selectAll, flexItem, selectInvert, flexItem, selectClear, flexItem, flexItem, nil]]; 
    self.mCellNib = [UINib nibWithNibName:@"WebsiteViewCell" bundle:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.mTmpCell = nil;
    self.mCellNib = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)selectConfirmAction:(id)sender  
{ 
    [self.navigationController setToolbarHidden:YES animated:NO];
//    [self.navigationController popToRootViewControllerAnimated:YES];
    
//    [self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -4)] animated:YES];

    MyQYaViewController *secondView = [[MyQYaViewController alloc] init];  
    [self.navigationController pushViewController:secondView animated:YES];  
    secondView.title = @"我的Q芽";  
} 

//好友全选
-(void)selectAllAction:(id)sender  
{  
    
}

//好友反选
-(void)selectInvert:(id)sender  
{  
    
}

//清除选择
-(void)selectClearAction:(id)sender  
{  
    
}


#pragma mark Table view data source and delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"";
}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    
	return [mWebsiteIntroArray count];
}


-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    
    static NSString *kCustomCellID = @"WebsiteViewCell";
	
	WebsiteViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
    
	if (cell == nil)
	{
        [self.mCellNib instantiateWithOwner:self options:nil];
        cell = mTmpCell;
        self.mTmpCell = nil;
	}
    
    cell.mWebsiteName.text = [mWebsiteIntroArray objectAtIndex:indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
	
	return cell;
}


- (IBAction)onClickEnterWebsiteIntro:(id)sender
{
    WebsiteIntroViewController *secondView = [[WebsiteIntroViewController alloc] init];
    NSArray *webIntroContent = [NSArray arrayWithObjects:@"",@"",@"36ke网站标题",@"简介",@"关键词1",@"关键词2",@"关键词3", nil];
    [secondView setViewContent:webIntroContent];
    secondView.title = @"36KR";
    [self.navigationController pushViewController:secondView animated:YES]; 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    WebsiteViewCell *cell = (WebsiteViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.mCheckBox.selected = !cell.mCheckBox.selected;
}


- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    
    WebsiteIntroViewController *secondView = [[WebsiteIntroViewController alloc] init];
    NSArray *webIntroContent = [NSArray arrayWithObjects:@"",@"",@"36ke网站标题",@"简介",@"关键词1",@"关键词2",@"关键词3", nil];

    secondView.title = @"36KR";
    
    //    if (childController == nil) { 
    //        childController = [[MovieDetailViewController alloc]
    //                           initWithNibName:@"MovieDetailViewController" bundle:nil]; 
    //    } 
//    //    NSUInteger row = [indexPath row]; 
    //    NSString *selectedMovie = [movieList objectAtIndex:row]; 
    //    NSString *detailMessage = [[NSString alloc]
    //                               initWithFormat:@"你选择了电影：%@.", selectedMovie]; 
    //    childController.message = detailMessage; 
    //    childController.title = selectedMovie; 
    [self.navigationController pushViewController:secondView animated:YES];
    NSUInteger row = [indexPath row];
    secondView.title = [mWebsiteIntroArray objectAtIndex:row];
    [secondView.wholePic setImage:[UIImage imageNamed:[websitePicArray objectAtIndex:row]]];
}

-(void) viewWillDisappear:(BOOL)animated    
{   
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)    
    {   
        // back button was pressed.  We know this is true because self is no longer   
        // in the navigation stack.     
        NSLog(@"viewWillDisappear:(BOOL)animated ");   
        [self.navigationController setToolbarHidden:YES animated:NO];

    }   
    [super viewWillDisappear:animated];   
} 


@end
